package com.example.alihomepage;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {
    private AppBarLayout appBar;
    private LinearLayout llTwo;
    private LinearLayout llThree;
    private LinearLayout llOne;
    private Toolbar toolbar;
    private ImageView ivSan;
    private TextView tvScan;
    private ImageView ivPay;
    private TextView tvPay;
    private ImageView ivCharge;
    private TextView tvCharge;
    private ImageView ivCard;
    private TextView tvCard;
    private ImageView ivOne;
    private ImageView ivTwo;
    private ImageView ivThree;
    private ImageView ivFour;
    private ImageView ivFive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        appBar = (AppBarLayout) findViewById(R.id.appBar);
        appBar.addOnOffsetChangedListener(this);
        llTwo = (LinearLayout) findViewById(R.id.ll_two);
        llThree = (LinearLayout) findViewById(R.id.ll_three);
        llOne = (LinearLayout) findViewById(R.id.ll_one);
        ivSan = (ImageView) findViewById(R.id.iv_san);
        tvScan = (TextView) findViewById(R.id.tv_scan);
        ivPay = (ImageView) findViewById(R.id.iv_pay);
        tvPay = (TextView) findViewById(R.id.tv_pay);
        ivCharge = (ImageView) findViewById(R.id.iv_charge);
        tvCharge = (TextView) findViewById(R.id.tv_charge);
        ivCard = (ImageView) findViewById(R.id.iv_card);
        tvCard = (TextView) findViewById(R.id.tv_card);
        ivOne = (ImageView) findViewById(R.id.iv_one);
        ivTwo = (ImageView) findViewById(R.id.iv_two);
        ivThree = (ImageView) findViewById(R.id.iv_three);
        ivFour = (ImageView) findViewById(R.id.iv_four);
        ivFive = (ImageView) findViewById(R.id.iv_five);

    }

    //AppBarLayout的监听方法
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        Log.d("aaa", "verticalOffset=" + verticalOffset);
        //720*1080手机 verticalOffset取值范围[0-200]px
        float absVerticalOffset = Math.abs(verticalOffset);//AppBarLayout竖直方向偏移距离px
        float totalScrollRange = appBarLayout.getTotalScrollRange();//AppBarLayout总的距离px
        Log.e("MainActivity", "absVerticalOffset =  "+absVerticalOffset+":"+"totalScrollRange = "+totalScrollRange );
        //滑动超出了1/10
        if(absVerticalOffset>=(totalScrollRange/10)){
            llOne.setVisibility(View.VISIBLE);
            llTwo.setVisibility(View.INVISIBLE);
        }
        else{
            llOne.setVisibility(View.INVISIBLE);
            llTwo.setVisibility(View.VISIBLE);
        }
        float px = absVerticalOffset/totalScrollRange;

        //设置渐变

        int argb = changeAlpha(getResources().getColor(R.color.white),1-px);

        ivSan.setAlpha(1-px);
        tvScan.setTextColor(argb);

        ivPay.setAlpha(1-px);
        tvPay.setTextColor(argb);

        ivCharge.setAlpha(1-px);
        tvCharge.setTextColor(argb);

        ivCard.setAlpha(1-px);
        tvCard.setTextColor(argb);


        ivOne.setAlpha(px);
        ivTwo.setAlpha(px);
        ivThree.setAlpha(px);
        ivFour.setAlpha(px);
        ivFive.setAlpha(px);

    }

    /** 根据百分比改变颜色透明度 */
    public int changeAlpha(int color, float fraction) {
        Log.e("MainActivity", "fraction: "+fraction );
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        int alpha = (int) (Color.alpha(color) * fraction);
        return Color.argb(alpha, red, green, blue);
    }


}
